home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gnulib / libsrc98.zoo / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-19  |  3.5 KB  |  185 lines

  1. /*
  2.  * device driver for the screen and mouse devices
  3.  */
  4.  
  5. #include <linea.h>
  6. #include <memory.h>
  7. #include <device.h>
  8. #include <errno.h>
  9. #include <osbind.h>
  10. #include <string.h>
  11.  
  12. #undef RED
  13. #undef GREEN
  14. #undef BLUE
  15. #include <screen.h>
  16. #include <mouse.h>
  17.  
  18. #define SCR_FD    mkdev(0x04, 0x01)
  19. #define MOUS_FD    mkdev(0x04, 0x02)
  20.  
  21. #define MULT (256/SCR_RGB_VALUES)
  22. #define RGB_B 0x000f
  23. #define RGB_G 0x00f0
  24. #define RGB_R 0x0f00
  25.  
  26. static long _scr_pos;        /* screen device read position */
  27. static int _mous_x, _mous_y;
  28.  
  29. static long scr_open(name, flags, mode)
  30.     char *name;
  31.     int flags;
  32.     unsigned mode;
  33. {
  34.     if (!strcmp(name, "SCR:")) {
  35.         (void)linea0();    /* initialize line A variables */
  36.         _scr_pos = 0;
  37.         return SCR_FD;
  38.     }
  39.     if (!strcmp(name, "MOUS:")) {
  40.         (void)linea0();
  41.         _mous_x = GCURX; _mous_y = GCURY;
  42.         return MOUS_FD;
  43.     }
  44.     errno = ENOENT;        /* shouldn't happen */
  45.     return -1;
  46. }
  47.  
  48. static long scr_close(fd)
  49. int fd;
  50. {
  51.     return 0;
  52. }
  53.  
  54. static long scr_read(fd, buf, nbytes)
  55.     int fd;
  56.     char *buf;
  57.     long nbytes;
  58. {
  59.     static struct mouse_buf mouse;
  60.     long cnt = 0;
  61.     char *from;
  62.  
  63.     if (fd == SCR_FD) {
  64.         cnt = ((long)V_X_MAX * V_Y_MAX * (long)VPLANES)/8;
  65.         cnt -= _scr_pos;
  66.         if (nbytes < cnt)
  67.             cnt = nbytes;
  68.         from = (char *)Logbase() + _scr_pos;
  69.         if (cnt < 0) return 0;
  70.         bcopy(from, buf, cnt);
  71.         _scr_pos += cnt;
  72.         return cnt;
  73.     }
  74.     if (fd == MOUS_FD) {
  75.         cnt = (nbytes < sizeof(mouse)) ? nbytes : sizeof(mouse);
  76.         mouse.m_buttons = MOUSE_BT;
  77.         mouse.m_dx = GCURX - _mous_x; _mous_x = GCURX;
  78.         mouse.m_dy = GCURY - _mous_y; _mous_y = GCURY;
  79.         from = (char *) &mouse;
  80.         nbytes = cnt;
  81.         while (cnt-- > 0)
  82.             *buf++ = *from++;
  83.         return nbytes;
  84.     }
  85.     errno = EBADF;
  86.     return -1;
  87. }
  88.  
  89. static long scr_write(fd, buf, nbytes)
  90.     int fd;
  91.     char *buf;
  92.     long nbytes;
  93. {
  94.     char *to;
  95.     long result;
  96.  
  97.     if (fd == SCR_FD) {
  98.         to = (char *)Logbase() + _scr_pos;
  99.         result = ((long)V_X_MAX*V_Y_MAX*(long)VPLANES)/8;
  100.         result -= _scr_pos;
  101.         if (nbytes < result) result = nbytes;
  102.         if (result <= 0) return 0;
  103.         bcopy(buf, to, result);
  104.         _scr_pos += result;
  105.         return result;
  106.     }
  107.     if (fd == MOUS_FD) {
  108.         errno = EACCESS;
  109.     }
  110.     else
  111.         errno = EBADF;
  112.     return -1;
  113. }
  114.  
  115. static long scr_ioctl(fd, func, arg)
  116.     int fd;
  117.     int func;
  118.     void *arg;
  119. {
  120.     short i, color;
  121.  
  122.     struct scr_param *p = (struct scr_param *)arg;
  123.     struct scr_palette *c = (struct scr_palette *)arg;
  124.  
  125.     if (fd != SCR_FD) {
  126.         errno = ENOTTY;
  127.         return -1;
  128.     }
  129.  
  130.     switch(func) {
  131.     case SCRGETPARAM:
  132.         p->scr_base = (long)Logbase();
  133.         p->scr_width = V_X_MAX;
  134.         p->scr_height = V_Y_MAX;
  135.         p->scr_depth = VPLANES;
  136.         p->scr_mode = Getrez();
  137.         break;
  138.     case SCRSETPARAM:
  139.         Setscreen(-1L, -1L, p->scr_mode);
  140.         Setscreen(p->scr_base, p->scr_base, -1);
  141.         break;
  142.     case SCRGETCOLOR:
  143.         for (i = 0; i < SCR_NUM_COLORS; i++) {
  144.             color = Setcolor(i, -1);
  145.             c->scr_rgb[i][BLUE] = MULT * (color & RGB_B);
  146.             c->scr_rgb[i][GREEN] = MULT * ((color & RGB_G) >> 4);
  147.             c->scr_rgb[i][RED] = MULT * ((color & RGB_R) >> 8);
  148.         }
  149.         break;
  150.     case SCRSETCOLOR:
  151.         for(i = 0; i < SCR_NUM_COLORS; i++) {
  152.             color = c->scr_rgb[i][RED] / MULT;
  153.             color = color << 4;
  154.             color |= c->scr_rgb[i][GREEN] / MULT;
  155.             color = color << 4;
  156.             color |=  c->scr_rgb[i][BLUE] / MULT;
  157.             (void)Setcolor(i, color);
  158.         }
  159.         break;
  160.     default:
  161.         errno = EINVAL;
  162.         return -1;
  163.     }
  164.     return 0;
  165. }
  166.  
  167. static struct _device scrn = {
  168.     "SCR:", "screen", SCR_FD,
  169.     scr_open, scr_close, scr_read, scr_write, scr_ioctl,
  170.     0
  171. };
  172.  
  173. static struct _device mouse = {
  174.     "MOUS:", "mouse", MOUS_FD,
  175.     scr_open, scr_close, scr_read, scr_write, scr_ioctl,
  176.     0
  177. };
  178.  
  179. void
  180. install_screen()
  181. {
  182.     _install_device(&scrn);
  183.     _install_device(&mouse);
  184. }
  185.